package com.xyhuo.leetcode.january;


import com.xyhuo.leetcode.common.TreeNode;

/**
 * 平衡二叉树
 * https://leetcode.cn/problems/balanced-binary-tree/
 *
 * @time 2024/1/9
 */
public class IsBalanced {

    public boolean isBalanced(TreeNode root) {
        search(root);
        return ans;
    }

    private boolean ans = true;

    private int search(TreeNode root) {
        if (null == root) {
            return 0;
        }
        int leftDepth = search(root.left);
        int rightDepth = search(root.right);
        if (Math.abs(leftDepth - rightDepth) > 1) {
            ans = false;
        }
        return Math.max(leftDepth, rightDepth) + 1;
    }

}
